import Taro, {Component} from '@tarojs/taro'
import {WebView} from '@tarojs/components'
import { connect } from '@tarojs/redux'

@connect(({web}) => ({
  url: web.url
}))
export default class Web extends Component {

  config = {
    navigationBarTitleText: ''
  }

  state = {
    redirectUrl: ''
  }

  componentWillMount = () => {
    const { title, redirectUrl } = this.$router.params
    if (title) {
      wx.setNavigationBarTitle({
        title
      })
    }
    redirectUrl && this.setState({redirectUrl})
  }

  componentWillUnmount = () => {
    const { redirectUrl } = this.state
    if (redirectUrl) {
      Taro.reLaunch({url: redirectUrl})
    }
  }

  handleMessage = (res) => {
    const {data} = res.detail
    if (data.includes('home')) {
      Taro.reLaunch({url: "/pages/taskSquare/index"})
    }
  }

  render () {
    const { url } = this.props
    console.log('----------------web---------------\n' + url)
    return (
      <WebView src={url} onMessage={this.handleMessage}/>
    )
  }
}
